<?php
// +----------------------------------------------------------------------
// | Desc: [ Better-卡分组 ]
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\index\model;

use think\facade\Db;
use app\common\model\Common;

class UserToken extends Common
{
    /**
     * @param $data
     */
    public function setToken($data)
    {
        $ip     = ip();
        $token  = setToken($ip);
        Db::name('user_token')->save([
            'user_id'   => $data['user_id'],
            'token'     => $token,
            'ip'        => $ip,
            'ctime'     => getUtcTime()
        ]);
        return $token;
    }

    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['user_id', '=', $post['user_id']];

        $list = Db::name('user_token')
            ->where($where)
            ->field('ip,ctime')
            ->order('ctime desc')
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }


}